<?php
include_once('DBConnection.php');
include_once('ClassBase.php');

	class DAOHandler extends DBConnection{
		    public $cb_name="";

			function __construct($cb_n) {
				$this->cb_name=$cb_n;
			}

			function insert($obj_classBase){
				$conn = $this->getConnection();
				

				$tsql = "insert into ".$this->cb_name." (";

				foreach ($obj_classBase->list_fields as $campo){
					$key= array_search($campo, $obj_classBase->list_fields);
					$tsql= $tsql.$key.",";

				}

				$tsql = substr($tsql, 0, strlen($tsql)-1);

				$tsql= $tsql.") values (";


				foreach ($obj_classBase->list_fields as $campo){
					$tsql = $tsql."?,";
				}

				$tsql = substr($tsql, 0, strlen($tsql)-1).")";

				//echo "SQL: ".$tsql."<br>";

				$params = array();

				$i=0;

				foreach ($obj_classBase->list_fields as $campo){					
					$params[$i]=$campo;
					$i++;
				}

				//echo "Params: ".json_encode($params)."<br>";

				$result = $this->execute_query( $conn, $tsql, $params);
				
				if(!$result){
					echo "No se pudo insertar en la tabla ".$obj_classBase->cb_name;					
				}

				$this->free_stmt( $result);
				$this->close($conn);
			}

			function update($obj_classBase){

			}

			function delete($obj_classBase){

			}

			function getPrimaryKey($obj_classBase){

			}		
	}
?>